<!DOCTYPE HTML>
<html>
<head>
<?php
	require("components/helper.php");
	include("components/metadata.php");
?>
<title>40Towers - Home Page</title>
<link rel="stylesheet" href="css/global.css" />
<link rel="stylesheet" href="css/homeNews.css" />
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<?php
	include("components/analytics.php");
	include("components/database.php");	
?>
</head>
<body>

<div id="page">
    <?php
		include("components/header.php");
		include("components/nav.php");
	?>
    
    <div>
    <section class="group">
        <h1>40Towers - An unofficial Fawlty Towers fan site</h1>
        <article>
            	<?php 
					if ($result = $mysqli->query("SELECT * FROM NewsItem ORDER BY postDate DESC LIMIT 24")) { 
						while($obj = $result->fetch_object()){ 
							$id = $obj->id; 
							$title = $obj->title; 
	//						$bodyCopy = $obj->bodyCopy;
							$bodyCopy = substr($obj->bodyCopy, 0, strpos($obj->bodyCopy, '.', 300)) . "."; 
							$postDate = $obj->postDate;
							
							$datetime = str_replace(" ", "T", $postDate);
							
							$sourceUrl = $obj->sourceUrl;
							
							$imageUrl = "img/missing_image.gif";
							
							if ($result2 = $mysqli->query("SELECT * FROM image WHERE id=".$obj->imageId )) { 
								while($obj2 = $result2->fetch_object()){ 
									$imageUrl = "img/" . $obj2->url;
								}
							}
				?>
                <header>
                
                <h3>
					<a href='news_item.php?id=<?= $id; ?>'>Full&nbsp;Story&nbsp;&raquo;</a>
                </h3>
                <h2><a href='news_item.php?id=<?= $id; ?>'><?php echo replaceSpeechMarks($title); ?></a></h2>
                
                <p>
                <?php if ( $imageUrl != "img/missing_image.gif" ) { ?>
                	<a href='news_item.php?id=<?= $id; ?>'><img src="<?= $imageUrl; ?>" alt="<?php echo replaceSpeechMarks($title); ?>" /></a>
				<?php } ?>
				<?php echo nl2br(replaceSpeechMarks($bodyCopy)); ?>
                </p>
				
                <h3>
					<?php if ( $sourceUrl!="#" ) { ?>
                        <a href="<?= $sourceUrl; ?>" target="_blank">Source</a>
                    <?php } ?>
                </h3>
                    
            </header>
            <footer>Posted on <time datetime="<?= $datetime; ?>"><?php echo date('l jS \of F Y', strtotime($postDate) ); ?></time></footer>
            <hr>
					<?php
							} 
						}
					?>
        </article>
    </section>
    </div>
    <?php
		include("components/rightBanners.php");
	?>
</div>
<?php
	include("components/footer.php");
?>
</body>
</html>